package com.jeecg.minidao.aspect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MinidaoInterceptor implements EmptyInterceptor{
	
	public boolean onInsert(Field[] fields, Object obj) {

		Map<Object,Object> map = new HashMap<Object, Object>();
		for(int j=0;j<fields.length;j++){
			fields[j].setAccessible(true);
			
			String fieldName = fields[j].getName();
			
			if("createBy".equals(fieldName)){
				
				map.put("createBy", "scott");
			}
			if("createDate".equals(fieldName)){
				
				map.put("createDate", new Date());
			}
			try {
				
				setFieldValue(map, obj);
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			
			
			
			
			
		}
		
		
		
		return false;
	}
	
	
	
	public static void setFieldValue(Map<Object,Object> map,Object bean) throws NoSuchMethodException, RuntimeException, IllegalAccessException, InvocationTargetException{
		
		Class<?> cls = bean.getClass();
		
		Method[] methods = cls.getDeclaredMethods();
		Field[] fields = cls.getDeclaredFields();
		
		for(Field field : fields){
			
			String fldtype = field.getType().getSimpleName();
			String fldSetName = field.getName();
			
			String setMethod = praseSetName(fldSetName);
			
			if(!checkMethod(methods, setMethod)){
				
				continue;
			}
			Object value = map.get(fldSetName);
			
			System.out.println(value.toString());
			
			Method method = cls.getMethod(setMethod, field.getType());
			
			if(null!=value){
				
				if("String".equals(fldtype)){
					
					method.invoke(bean, (String)value);
				}else if("Double".equals(fldtype)){
					
					method.invoke(bean,(Double)value);
					
				}else if("int".equals(fldtype)){
					
					int val = Integer.valueOf((String)value);
					method.invoke(bean, val);
				}else{
					
					method.invoke(bean, value);
				}
				
				
				
				
				
			}
			
			
			
			
			
			
		}
		
		
		
		
		
		
		
	}
	
	
	public static boolean checkMethod(Method[] methods ,String met){
		
		if(null!=methods){
			
			for(Method method : methods){
				
				if(met.equals(method.getName())){
					
					return true;
				}
			}
			
			
		}
		return false;
	}
	
	
	public static String praseSetName(String fldname){
		
		if(null==fldname||"".equals(fldname)){
			
			return null;
		}
		String pro = "set"+fldname.substring(0,1).toUpperCase()+fldname.substring(1);
		return pro;
		
		
	}
	
	
	
	
	
	
	
	
	
	public boolean onUpdate(Field[] fields, Object obj) {
		// TODO Auto-generated method stub
		return false;
	}
}
